/***************************************************************
 * FILENAME:  lcd3200.c
 * AUTHOR:    zimmermane@msoe.edu <Evan Zimmerman>
 *            duerk@msoe.edu <Kevin Duer>
 * DATE:      14 Sept 2012
 * PROVIDES:  
 *    - LCD Device Driver 
 *    - Contains subroutines and functions for the LCD
 *   
 * tccr2: FOC2 WGM20 COM21 COM20 WGM21 CS22 CS21 CS20
 *
 * tifr: OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0
 **************************************************************/

#include "lcd3200.h"
#include "usart3200.h"
#include "helpers.h"
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>

/*
 * lcd_init
 * Purpose: Initializes the LCD display
 *			Default: LCD on, Cursor off, No blink
 * Parameters: None
 * Returns: None
 */
void lcd_init(void){
   lcd_send_data(0x16);
}

/*
 * lcd_gotoxy
 * Purpose: Moves to a specific location on the LCD panel
 * Parameters: Row: 0-1
 *			   Column 0-15
 * Returns: None
 */
void lcd_gotoxy(unsigned char row, unsigned char column){
   if(row == 0)
      lcd_send_data(0x80 + column);   
   if(row == 1)
      lcd_send_data(0x94 + column);
}

/*
 * lcd_init_heartbeat_icon
 * Purpose: Initializes a heart icon to the first custom character slot
 * Parameters: None
 * Returns: None
 */
void lcd_init_heartbeat_icon(void){
   lcd_send_data(248); // Define 1st Custom Character
   lcd_send_data(0);   //%00000
   lcd_send_data(0);   //%00000 
   lcd_send_data(27);  //%11011 ** **
   lcd_send_data(31);  //%11111 *****
   lcd_send_data(31);  //%11111 *****
   lcd_send_data(14);  //%01110  ***
   lcd_send_data(4);   //%00100   *
   lcd_send_data(0);   //%00000
   delay_ms(20); 
}

/*
 * lcd_init_antenna_icons
 * Purpose: Initializes antenna icons
 *			third custom character slot: antenna on
 *			fifth custom character slot: antenna off
 * Parameter: None
 * Returns: None
 */
void lcd_init_antenna_icons(void){
	//build the antenna on signal
	usart_send_data(0xFA);	//define character 3
	usart_send_data(1);		//    *
	usart_send_data(1);		//    *
	usart_send_data(1);		//    *
	usart_send_data(5);		//  * *
	usart_send_data(5);		//  * *
	usart_send_data(5);		//  * *
	usart_send_data(21);	//* * *
	usart_send_data(21);	//* * *
	delay_ms(20);			//delay for send
	
	//build the antenna off signal
	usart_send_data(0xFC);	//define character 5
	usart_send_data(0);		//
	usart_send_data(0);		//
	usart_send_data(0);		//
	usart_send_data(0);		//
	usart_send_data(0);		//
	usart_send_data(0);		//
	usart_send_data(16);	//*
	usart_send_data(16);	//*
	delay_ms(20);			//delay for send
}

/*
 * lcd_send_data
 * Purpose: sends a byte to the UDR to be transmitted.
 *          the '\r' character will move the cursor to 
 *          the beginning of the first row. The '\n' 
 *			moves the cursor to the beginning of the 
 *			second line;
 * Parameters: theData: byte to be sent to the UDR
 * Returns: None
 */
void lcd_send_data(unsigned char theData){
	delay_ms(5);
	usart_send_data(theData);
}

/*
 * lcd_send_string
 * Purpose: sends a string to the LCD
 * Parameters: str: the string to be sent
 * Returns: None
 */
void lcd_send_string(unsigned char * str){
	unsigned char i = 0;
	char c = str[i];
	while(c != 0x00){
		lcd_send_data(c);
		i++;
		c = str[i];
	}
}